import Index from '@/views/index.vue'
import { useCookies } from '@vueuse/integrations/useCookies'
import { createRouter, createWebHistory } from 'vue-router'
import http from '@/http'
import NotFound from '@/views/NotFound.vue'
import { useUserStore } from '@/stores/user'
import Login from '@/views/Login.vue'
import RegisteredList from '@/views/Registered list.vue'
import Checklist from '@/views/checklist.vue'
import Docheck from '@/views/docheck.vue'
import Dept from '@/views/dept.vue'
import Checkresult from '@/views/checkresult.vue'
import Registered from '@/views/registered.vue'
import { backtopEmits } from 'element-plus'
import Dispensing from '@/views/dispensing.vue'
import Backfeelist from '@/views/backfeelist.vue'
import Backfee from '@/views/backfee.vue'
import Chargelist from '@/views/chargelist.vue'
import Docharge from '@/views/docharge.vue'
import Myscheduling from '@/views/myscheduling.vue'
import Dict from '@/views/dict.vue'
import Notice from '@/views/notice.vue'
import Log_opt from '@/views/log_opt.vue'
import Log_login from '@/views/log_login.vue'
import Ins_fee from '@/views/ins_fee.vue'
import Reg_fee from '@/views/reg_fee.vue'
import User from '@/views/User.vue'
import Role from '@/views/Role.vue'
import Menu from '@/views/Menu.vue'
import Scheduling from '@/views/scheduling.vue'
import Patient from '@/views/patient.vue'
import Newcare from '@/views/newcare.vue'
import Purchase from '@/views/purchase.vue'
import Producter from '../views/producter.vue'
import Medicinal from '@/views/medicinal.vue'
import Provider from '@/views/provider.vue'
import Examine from '@/views/examine.vue'
import Inventory from '@/views/inventory.vue'
import Insert from '@/views/Insert.vue'
import Home from '@/views/home.vue'
// import HomeView from '../views/HomeView.vue'
// 路由
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect:'/login',
    },
    {
      path: '/login',
      name: 'login',
      component:Login,
    },
    {
      path: '/index',
      name: 'index',
      component: Index,
      //子路由配置
      children:[
        //如果路径没有对应组件，统一404 Not Found组件
        {
          path: '/:pathMatch(.*)*',
          name: '404',
          component:NotFound ,
        }, {
          path: '',
          name: 'home',
          component:Home ,
        },{
          path:'/doctor',
          name:'doctor',
          children:[
            {
              path: 'registeredlist',
              name: 'registeredlist',
              component:RegisteredList,
            },{
              path: 'registered',
              name: 'registered',
              component:Registered, 
            },{
              path:'myscheduling',
              name:'myscheduling',
              component:Myscheduling
            },{
              path:'scheduling',
              name:'scheduling',
              component:Scheduling
            },{
              path:'patient',
              name:'patient',
              component:Patient
            },{
              path:'newcare',
              name:'newcare',
              component:Newcare
            }
          ]
        },{
          path:'/check',
          name:'check',
          children:[
            {
              path: 'checklist',//检查结果查询
              name: 'checklist',
              component:Checklist,
            },{
              path: 'docheck',//检查结果查询
              name: 'docheck',
              component:Docheck,
            },{
              path: 'checkresult',//检查结果录入
              name: 'checkresult',
              component:Checkresult,
            }
          ]
        },{
          path:'/system',//系统管理父菜单
          name:'system',
          children:[
            {
              path: 'dept',//科室管理
              name: 'dept',
              component:Dept,
            },{
              path: 'dict',//字典
              name:'dict',
              component:Dict,
            },{
              path: 'notice',//通告
              name: 'notice',
              component:Notice,
            },{
              path: 'log_opt',//登录日志
              name: 'log_opt',
              component: Log_opt,
            },{
              path: 'log_login',//登录操作
              name: 'log_login',
              component: Log_login,
            },{
              path:'ins_fee',//检查费用
              name:'ins_fee',
              component:Ins_fee,
            },{
              path:'reg_fee',//挂号费用
              name:'reg_fee',
              component: Reg_fee,
            },{
              path:'user',//用户
              name:'user',
              component: User,
            },{
              path:'role',//角色
              name:'role',
              component: Role,
            },{
              path:'menu',//菜单
              name:'menu',
              component: Menu,
            }
          ]
        },{
          path:'/charge',//看病模块
          name:'charge',
          children:[
            {
              path: 'chargelist',//科室管理
              name: 'chargelist',
              component:Chargelist,//x
            }, {
              path: 'backfeelist',//科室管理
              name: 'backfeelist',
              component:Dispensing,
            }, {
              path: 'backfee',//科室管理
              name: 'backfee',
              component:Backfee,
            },{
              path: 'docharge',//处方收费
              name: 'docharge',
              component:Docharge,
            },{
              path: 'dispensing',//处方收费
              name: 'dispensing',
              component:Dispensing
            }
          ]
        },{ path:'/stock',//看病模块
          name:'stock',
          children:[
            {
              path: 'purchase',//科室管理
              name: 'purchase',
              component:Purchase,//x
            },
            {
              path: 'producter',//科室管理
              name: 'producter',
              component:Producter,//x
            },
            {
              path: 'medicinal',//科室管理
              name: 'medicinal',
              component:Medicinal,//x
            },
            {
              path: 'provider',//科室管理
              name: 'provider',
              component:Provider,//x
            },
            {
              path: 'examine',//科室管理
              name: 'examine',
              component:Examine,//x
            },
            {
              path: 'inventory',//科室管理
              name: 'inventory',
              component:Inventory,//x
            },{
              path: 'insert',//科室管理
              name: 'insert',
              component:Insert,//x
            },
          ]
        }
      ]
    },
  ],
})

//路由前置守卫
router.beforeEach(async(to,from)=>{
   //从cookie中获取token
   const cookie=useCookies()
   const token=cookie.get('token')
  //!Token为空,或不是访问登录，如果没有登录，强制跳转回登录
  if(token==undefined && to.path!="/login"){
    return '/login'
  }
   //判断如果目标路径不是/login,并且没有登录，跳转到登录先进行登录
  //  if(to.path!='/login'&&!uid){
  //     return '/login'
  //  }
   //如果登录
   if(token){
     //根据用户编号获取用户对象，存储到Pinia的用户数据单元
     await http.get('/user/getUser').then((res)=>{
      console.log("进入if判断")
         console.log(res)
         //获取用户数据单元对象
         const userStore=useUserStore()
         userStore.setUser(res)
     })
     //进入目标组件
   }
})

export default router
